package com.iqiyi.service.impl;

import com.iqiyi.mapper.IqiyiUserMapper;
import com.iqiyi.po.IqiyiUser;
import com.iqiyi.dto.LoginInfo;
import com.iqiyi.po.IqiyiUserExample;
import com.iqiyi.service.IqiyiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;

/**
 * TODO
 * 爱奇艺登录用户的业务层
 * @author Administrator
 * @date 2021/2/24 11:29
 */
@Service
@Slf4j
public class IqiyiUserServiceImpl implements IqiyiUserService {

    @Autowired
    private IqiyiUserMapper iqiyiUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public IqiyiUser selectByUid(Integer uid) {
        IqiyiUser iqiyiUser = iqiyiUserMapper.selectByPrimaryKey(uid);
        if (iqiyiUser!=null){
            return iqiyiUser;
        }else {
            return null;
        }
    }



    /**
     * 根据手机号查询用户信息
     * @param uphone
     * @return
     */
    @Override
    public List<IqiyiUser> selectIqiyiUsetByUphone(String uphone) {
        IqiyiUserExample iqiyiUserExample = new IqiyiUserExample();
        IqiyiUserExample.Criteria criteria = iqiyiUserExample.createCriteria();
        criteria.andUphoneEqualTo(uphone);
        List<IqiyiUser> iqiyiUsers = iqiyiUserMapper.selectByExample(iqiyiUserExample);
        return iqiyiUsers;
    }

    /**
     * 注册用户
     * @param iqiyiUser
     * @return
     */
    @Override
    public int insertIqiyiUSerByUphone(IqiyiUser iqiyiUser) {
        int i = iqiyiUserMapper.insertSelective(iqiyiUser);
        System.out.println(iqiyiUser);
        return i;
    }

    /**
     * 根据手机号和密码查询用户
     * @param loginInfo
     * @return
     */
    @Override
    public List<IqiyiUser> selectIqiyiUserByUphoneAndPassWord(LoginInfo loginInfo) {
        String uphone =loginInfo.getPhone();
        String pass = loginInfo.getPass();
        IqiyiUserExample iqiyiUserExample = new IqiyiUserExample();
        IqiyiUserExample.Criteria criteria = iqiyiUserExample.createCriteria();
        criteria.andUphoneEqualTo(uphone);
        criteria.andPasswordEqualTo(pass);
        List<IqiyiUser> iqiyiUsers = iqiyiUserMapper.selectByExample(iqiyiUserExample);

        if(iqiyiUsers != null && iqiyiUsers.size() > 0){
            redisTemplate.opsForValue().set("loginUser",iqiyiUsers.get(0));
            log.info("data set to redis sucess");
        }
        return iqiyiUsers;
    }

    /**
     * 删除redis数据库中的loginUser
     */
    @Override
    public void exitLoginUser() {
        redisTemplate.delete("loginUser");
    }

    @Override
    public IqiyiUser getUserInRedis() {
        Boolean aBoolean = redisTemplate.hasKey("loginUser");
        if(aBoolean == null){
            return null;
        }
        Object loginUser = redisTemplate.opsForValue().get("loginUser");
        return (IqiyiUser)loginUser;
    }


    @Override
    public IqiyiUser selectByuid(Integer uid) {
        IqiyiUser iqiyiUser = iqiyiUserMapper.selectByPrimaryKey(uid);
        return iqiyiUser;
    }
}
